<?php

class BrandApp extends MemberbaseApp
{
	var $_brand_mod;
    function __construct()
    {
        $this->BrandApp();
    }
    function BrandApp()
    {
        parent::__construct();
		$this->check_powers(USER_ADMIN);
		$this->_brand_mod =& m('brand');
    }
	
    function index()
    {

		$page = $this->_get_page();
		$brands = $this->_brand_mod->find(array(
			'order' => 'brand_id ASC',
			'count' => true,
			'join'	=>'belong_company',
			'limit' => $page['limit']
		));
		$page['item_count'] = $this->_brand_mod->getCount();
		$this->_format_page($page);
		$this->assign('page_info', $page);
		$this->assign('brands', $brands);
        $this->display('brand.index.html');
        /* 当前位置 */
        $this->_curlocal(LANG::get('brand_manage'));
    }
	
	function add()
	{
		if (!IS_POST) {
			$company_mod =& m('company');
			$companys =$company_mod->find();
			$this->assign('companys', $companys);
			$this->_assign_form();
	        $this->display('brand.form.html');
        	/* 当前位置 */
        	$this->_curlocal(LANG::get('brand_manage'), 'index.php?app=brand',
				Lang::get('add_new_brand'));
		} else {
			$brand_name = trim($_POST['brand_name']);
			$brand_addr = trim($_POST['brand_addr']);
			$description = trim($_POST['description']);
			$if_show = intval($_POST['if_show']);
			$company_id = intval($_POST['company_id']);
			
			if (empty($brand_name)) {
				$this->show_warning('brand_name_not_empty');
				return;
			}
//			if (empty($company_id)) {
//				$this->show_warning('company_not_empty');
//				return;
//			}
			if (!$this->_brand_mod->unique($brand_name)) {
				$this->show_warning('brand_name_is_exist');
				return;
			}
			
			$data = array(
				'brand_name' => $brand_name,
				'brand_addr' => $brand_addr,
				'description' => $description,
				'if_show' => $if_show,
				'company_id'=>$company_id
			);
			
			$brand_id = $this->_brand_mod->add($data);
			if (!$brand_id) {
				$this->_error($this->_brand_mod->get_error());
				return;
			}
			$this->show_message('add_brand_success', Lang::get('brand_manage'), 'index.php?app=brand');
		}
	}
	
	function _assign_form()
	{
		$brand = array(
			'if_show' => 1,
		);
		$this->assign('brand', $brand);
	}
	
	function edit()
	{
		$brand_id = intval($_GET['brand_id']);
		if (!$brand_id) {
			$this->show_warning('brand_not_exit');
		}
		if (!IS_POST) {
			$company_mod =& m('company');
			$companys =$company_mod->find();
			$this->assign('companys', $companys);
			$brand = $this->_brand_mod->get_info($brand_id);
			$this->assign('brand', $brand);
			
	        $this->display('brand.form.html');
        	/* 当前位置 */
        	$this->_curlocal(LANG::get('brand_manage'), 'index.php?app=brands',
				Lang::get('edit_brand'));
		} else {
			$brand_name = trim($_POST['brand_name']);
			$brand_addr = trim($_POST['brand_addr']);
			$description = trim($_POST['description']);
			$if_show = intval($_POST['if_show']);
			$company_id = intval($_POST['company_id']);
			
			if (empty($brand_name)) {
				$this->show_warning('brand_name_not_empty');
				return;
			}
//			if (empty($company_id)) {
//				$this->show_warning('company_not_empty');
//				return;
//			}
			if (!$this->_brand_mod->unique($brand_name, $brand_id)) {
				$this->show_warning('brand_name_is_exist');
				return;
			}
			
			$data = array(
				'brand_name' => $brand_name,
				'brand_addr' => $brand_addr,
				'description' => $description,
				'if_show' => $if_show,
				'company_id'=>$company_id
			);
			$this->_brand_mod->edit($brand_id, $data);
			if ($this->_brand_mod->get_error()) {
				$this->_error($this->_brand_mod->get_error());
				return;
			}
			$this->show_message('edit_brand_success', Lang::get('brand_manage'), 'index.php?app=brand');
		}
	}

	function del()
	{
		$brand_id = intval($_GET['brand_id']);
		if (!$brand_id) {
			$this->show_warning('brand_not_exit');
			return;
		}

		$brand_mod=&m("brand");
		$brand_mod->drop("brand_id={$brand_id}");
		if ($brand_mod->get_error()) {
			$this->_error($brand_mod->get_error());
			return;
		}
		
		$this->show_message('删除品牌成功', Lang::get('brand_manage'), 'index.php?app=brand');
		
	}
		
	function check_brand()
	{
		$brand_name = empty($_GET['brand_name']) ? null : trim($_GET['brand_name']);
		$brand_id = empty($_GET['brand_id']) ? 0 : intval($_GET['brand_id']);
		$company_id = empty($_GET['company_id']) ? 0 : intval($_GET['company_id']);
		if (!$brand_name) {
            echo ecm_json_encode(false);
            return;
		}
		echo ecm_json_encode($this->_brand_mod->unique($brand_name,$brand_id, $company_id));
	}
}

?>
